package com.lxzh.demo.microservice.articleservice.controller;

import com.authenticate.client.AuthApi;
import com.authenticate.client.UserApi;
import com.authenticate.dto.UserRegisterDTO;
import com.authenticate.vo.LoginUserVO;
import com.supone.communal.response.DCResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : qun.zheng
 * @Description: TODO
 * @date : 2020/5/12 16:35
 **/
@RestController
@RequestMapping("/articles")
public class ArticleController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private AuthApi authApi;
    @Autowired
    private UserApi userApi;


    @GetMapping("remoteCall")
    public String remoteCall() {
        return restTemplate.getForObject("http://user-service/users/hello", String.class);
    }

    @GetMapping("instances/{serviceName}")
    public List<ServiceInstance> getInstanceInfos(@PathVariable("serviceName") String serviceName) {
        return discoveryClient.getInstances(serviceName);
    }

    @GetMapping("test/{serviceName}")
    public String test(@PathVariable("serviceName") String serviceName) {
        return "您好！";
    }


    @PostMapping("/register")
    public DCResponse<LoginUserVO> register(@RequestBody UserRegisterDTO user, @RequestParam String code, HttpServletRequest request) {
        LoginUserVO loginUserVO = authApi.register(user, code);
        return DCResponse.success(loginUserVO);
    }


}
